package com.ly.web;


import com.github.pagehelper.PageInfo;
import com.ly.annotation.RoleRequire;
import com.ly.dto.StudentClassDTO;
import com.ly.mapper.CultivateMapper;
import com.ly.po.TClassPO;
import com.ly.pojo.TClass;
import com.ly.result.R;
import com.ly.service.CultivateService;
import com.ly.service.TClassService;
import com.ly.service.TStudentClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Role;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/class")
public class ClassController {

    @Autowired
    private TClassService tClassService;

    @Autowired
    private TStudentClassService tStudentClassService;

    @Autowired
    private CultivateService cultivateService;


    /**
     * 获取班级列表（分页）
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/class")
    //@RoleRequire({"ADMIN","STUDENT"})
    @RoleRequire({"ADMIN"})
    public R getClassList(@RequestParam int page, @RequestParam int size) {
        PageInfo<TClassPO> pageInfo = tClassService.getTClassPO(page, size);
        return R.success(pageInfo.getList(), String.valueOf(pageInfo.getTotal()));
    }

    /**
     * 搜索班级
     */
    @GetMapping("/class/search/{searchText}")
    @RoleRequire("ADMIN")
    public R searchTClassList(@RequestParam int page, @RequestParam int size, @PathVariable String searchText) {
        PageInfo<TClass> pageInfo = tClassService.getSearchTClass(page, size, searchText);
        return R.success(pageInfo.getList(), String.valueOf(pageInfo.getTotal()));
    }

    /**
     * 删除班级
     * @param id
     * @return
     */
    @DeleteMapping("/class/{id}")
    @RoleRequire("ADMIN")
    public R deleteTClass(@PathVariable Integer id) {
        int i = tClassService.deleteTClassById(id);
        if(i != 1) {
            return R.error("error");
        }
        // 删除班级时，同时删除学生班级信息
        int i1 = tStudentClassService.deleteByClassId(id);
        return R.success("success");
    }

    /**
     * 新增班级
     * @param class
     * @return
     */
    @PostMapping("/class")
    @RoleRequire("ADMIN")
    public R insertTClass(@RequestBody TClassPO tClass) {
        int i = tClassService.insertTClass(tClass);
        if (i == 2) {
            return R.error("班级代码重复");
        }
        if(i != 1) {
            return R.error("error");
        }
        return R.success("success");
    }

    /**
     * 更新班级信息
     * @param class
     * @return
     */
    @PutMapping("/class")
    @RoleRequire("ADMIN")
    public R updateTClass(@RequestBody TClassPO tClass) {
        int i = tClassService.updateTClass(tClass);
        if(i == 2){
            //学号重复
            return R.error("班级代码重复");
        }else if (i != 1) {
            return R.error("error");
        }
        return R.success("success");
    }

    /**
     * 为班级批量添加学生
     * @return
     */
    @PostMapping("/class/addStudents")
    @RoleRequire("ADMIN")
    public R addStudents(@RequestBody StudentClassDTO studentClassDTO) {
        Long i = tStudentClassService.insertStudentClassBySnoList(studentClassDTO.getClassCode(), studentClassDTO.getClassName(), studentClassDTO.getSelectedSnoList());
        // 通过班级代码拿到课程代码列表
        List<String> courseCodeList = cultivateService.getCourseCodeListByClassCode(studentClassDTO.getClassCode());
        cultivateService.selectRequiredCourse(studentClassDTO.getClassCode(), courseCodeList);
        if (i.equals(1L)) {
            return R.success(null);
        } else {
            return R.error(500, i + "");
        }
    }

    /**
     * 为班级批量删除学生
     * @param studentClassDTO
     * @return
     */
    @PostMapping("/class/deleteStudents")
    @RoleRequire("ADMIN")
    public R deleteStudents(@RequestBody StudentClassDTO studentClassDTO) {
        Long i = tStudentClassService.deleteBySnoList(studentClassDTO.getSelectedSnoList(), studentClassDTO.getClassCode());
        // 通过班级代码拿到课程代码列表
        List<String> courseCodeList = cultivateService.getCourseCodeListByClassCode(studentClassDTO.getClassCode());
        cultivateService.deleteRequiredCourse(studentClassDTO.getClassCode(), courseCodeList);
        if (i.equals(1L)) {
            return R.success(null);
        } else {
            return R.error(500, i + "");
        }
    }

}
